﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VietImage.Models
{
    public partial class Country
    {
        #region Properties
        #endregion

        #region  Methods
        #endregion

        #region Static Methods
        public static List<Country> GetList()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Countries.OrderBy(p => p.Name);
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        private static List<ErrorItem> InputInfomationValidation(Country country)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();
            if (country.Name == "")
                errorList.Add(new ErrorItem("Name", "Country Name is required."));
            if (country.Name.Length > 15)
                errorList.Add(new ErrorItem("Name", "Country Name is only allow up to 15 characters."));
            return errorList;
        }

        public static List<ErrorItem> Insert(ref Country country)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            List<ErrorItem> errorList = InputInfomationValidation(country);
            if (errorList.Count == 0)
            {
                System.Object ob = (System.Object)country;
                if (!Provider.Insert(ref ob, "Countries"))
                    errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Where(p => p.ID_ErrorMessage == 55).First().Description));
            }
            return errorList;
        }

        public static List<ErrorItem> Delete(int id_Country)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            List<ErrorItem> errorList = new List<ErrorItem>();

            Country di = _db.Countries.Single(p => p.ID == id_Country);
            if (Provider.Delete(di, "Countries"))
                errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Where(p => p.ID_ErrorMessage == 1).First().Description));
            else
                errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Where(p => p.ID_ErrorMessage == 54).First().Description));

            return errorList;
        }
        #endregion
    }
}